package com.ruoyi.yf.service.impl;

import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.ruoyi.yf.domain.interfaces.YfDepartment;
import com.ruoyi.yf.domain.mobile.YfMobileDeptNurse;
import com.ruoyi.yf.mapper.YfTagMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.yf.mapper.YfDeptMapper;
import com.ruoyi.yf.domain.YfDept;
import com.ruoyi.yf.service.IYfDeptService;
import com.ruoyi.common.core.text.Convert;

/**
 * 医院科室Service业务层处理
 *
 * @author XiaoYi...
 * @date 2020-03-11
 */
@Service
public class YfDeptServiceImpl implements IYfDeptService {

    private static final Logger log = LoggerFactory.getLogger(YfDeptServiceImpl.class);
    @Autowired
    private YfDeptMapper yfDeptMapper;

    @Autowired
    private YfTagMapper yfTagMapper;

    /**
     * 导入用户数据
     *
     * @param userList        用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importDept(List<YfDept> deptList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(deptList) || deptList.size() == 0) {
            throw new BusinessException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (YfDept dept : deptList) {
            try {
                // 验证是否存在这个科室
                    dept.setCreateBy(operName);
                    this.insertYfDept(dept);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、科室 " + dept.getDeptName() + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、科室 " + dept.getDeptName()+ " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 通过科室id 获取当前机构下所有科室
     * @param deptId 科室id
     * @param types 类型
     * @return
     */
    @Override
    public List<YfDept> selectYfDeptListByDeptId(Long deptId,String types) {
        return yfDeptMapper.selectYfDeptListByDeptId(deptId,Convert.toStrArray(types));
    }

    /**
     * 查询医院科室
     *
     * @param deptId 医院科室ID
     * @return 医院科室
     */
    @Override
    public YfDept selectYfDeptById(Long deptId) {
        return yfDeptMapper.selectYfDeptById(deptId);
    }

    /**
     * 查询医院科室列表
     *
     * @param yfDept 医院科室
     * @return 医院科室
     */
    @Override
    public List<YfDept> selectYfDeptList(YfDept yfDept) {
        return yfDeptMapper.selectYfDeptList(yfDept);
    }

    /**
     * 新增医院科室
     *
     * @param yfDept 医院科室
     * @return 结果
     */
    @Override
    public int insertYfDept(YfDept yfDept) {
        yfDept.setCreateTime(DateUtils.getNowDate());
        return yfDeptMapper.insertYfDept(yfDept);
    }

    /**
     * 修改医院科室
     *
     * @param yfDept 医院科室
     * @return 结果
     */
    @Override
    public int updateYfDept(YfDept yfDept) {
        yfDept.setUpdateTime(DateUtils.getNowDate());
        return yfDeptMapper.updateYfDept(yfDept);
    }

    /**
     * 删除医院科室对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteYfDeptByIds(String ids) {
        return yfDeptMapper.deleteYfDeptByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除医院科室信息
     *
     * @param deptId 医院科室ID
     * @return 结果
     */
    @Override
    public int deleteYfDeptById(Long deptId) {
        return yfDeptMapper.deleteYfDeptById(deptId);
    }

    /**
     * 获取用户下的医院 或分区下的科室
     *
     * @param uid 用户id
     * @return
     */
    @Override
    public List<YfDept> getUserIdDepartment(Long uid) {
        return yfDeptMapper.getUserIdDepartment(uid);
    }

    // 获取科室及对应的标准科室
    @Override
    public List<YfDept> getDepartmentAndStandardDeptByUserId(Long uid) {
        return yfDeptMapper.getDepartmentAndStandardDeptByUserId(uid);
    }

    /**
     * 通过暂存点id 获取医院 科室
     *
     * @param stagingId
     * @return
     */
    @Override
    public List<YfDept> selectHD(Long stagingId) {
        return yfDeptMapper.selectHD(stagingId);
    }

    @Override
    public List<YfDept> select_sysDept_yfDeptList(Long deptId) {
        return yfDeptMapper.select_sysDept_yfDeptList(deptId);
    }

    @Override
    public int updateRremoveAndRecoveryTagByIds(String ids, String status) {
        return yfDeptMapper.updateRremoveAndRecoveryTagByIds(Convert.toStrArray(ids), status);
    }

    @Override
    public int selectDeptTagSum(String ids) {
        return yfTagMapper.selectDeptTagSum(Convert.toLongArray(ids));
    }


    /**
     * 根据用户获取所在医院的科室护士列表
     */
    @Override
    public List<YfMobileDeptNurse> getKSNuserListByUid(Long uid) {
        return yfDeptMapper.getKSNuserListByUid(uid);
    }


    // 今天无医废录入
    @Override
    public int todayNoYF(Long stagingId) {
        return yfDeptMapper.todayNoYF(stagingId);
    }

    // 根据医院获取所有科室
    @Override
    public List<YfDept> getYfDeptListByHospitalId(Long hosid) {
        return yfDeptMapper.getYfDeptListByHospitalId(hosid);
    }

    // 根据登录名(医院管理员)获取医疗机构的科室列表
    @Override
    public List<YfDepartment> getKslistByLoginName(String userName) {
        return yfDeptMapper.getKslistByLoginName(userName);
    }

    // 根据暂存点查询医院所有科室
    @Override
    public List<YfDept> getKSlistByStagingId(Long stagingId) {
        return yfDeptMapper.getKSlistByStagingId(stagingId);
    }

}
